package ihuoui.mq.store.test;

import java.io.File;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import ihuoui.mq.store.CommitLog;
import ihuoui.mq.store.Mess;
import ihuoui.mq.store.StoreException;

public class CommitLogThread2FileMulti {
	final static CommitLog commitLog = new CommitLog();
	final static Integer total = 100*10000;
	final static Integer round=5;
	final static Date start=new Date();
	static AtomicLong writeNo=new AtomicLong(0l);
	static AtomicLong readNo=new AtomicLong(0l);
	static volatile Long  readStart=0l;

	@After
	@Before
	public void clealFile() {
		DeleteDirectory.deleteDir(new File("target/unit_test_store/MappedFileTest/"));
	}

	@SuppressWarnings("static-access")
	@Test
	public void testAppendMessageMulti() {
		System.out.println("start time:"+new Date());
		for (int i = 0; i < round; i++) {
			WrtiteRun wrtiteRun=new WrtiteRun("T");
			Thread thread=new Thread(wrtiteRun);
			thread.start();
		}
		try {
			Thread.currentThread().sleep(1*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < round; i++) {
			ReadRun wrtiteRun=new ReadRun("T");
			Thread thread=new Thread(wrtiteRun);
			thread.start();
		}
		try {
			Thread.currentThread().sleep(2*60*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("write:"+writeNo.get());
		System.out.println("read:"+readNo.get());
	}
class WrtiteRun implements Runnable {

	public String topic;
	
	public WrtiteRun(String topic) {
		this.topic=topic;
	}
	
	@Override
	public void run() {
		
		try {
			for (int i = 0; i < total; i++) {
				Mess mess = new Mess();
				mess.setTopic(topic);
				mess.setBody("Once, there was a chance for me!".getBytes());
				mess.setTag(""+writeNo.getAndIncrement());
//				System.out.println("write:"+i);
				commitLog.appendMessage("target/unit_test_store/MappedFileTest/", mess);
//				writeNo.getAndIncrement();
//				System.out.println("T1 write i:" + i);
			}
		} catch (StoreException e1) {
			e1.printStackTrace();
		}
		if (writeNo.get()>=(round*total-1)) {
			System.out.println("write end time:"+new Date());
			System.out.println("write use sec:"+(new Date().getTime()-start.getTime())/1000);
		}
//		System.out.println("write end time:"+new Date());
//		System.out.println("write use sec:"+(new Date().getTime()-start.getTime())/1000);
	}
};

class ReadRun implements Runnable {

	public String topic;
	
	public ReadRun(String topic) {
		this.topic=topic;
	}
	
	@Override
	public void run() {
		try {
			for (int i = 0; i < total; ) {
//				System.out.println("T1 read i:" + i);
				Mess read = commitLog.offerNextMessage("target/unit_test_store/MappedFileTest/", topic);
				if (read!=null) {
					i++;
					System.out.println("i:"+i+" read:"+read.getTag()+" no:"+readNo.get());
					readNo.getAndIncrement();
				}
			}
		} catch (StoreException e) {
			e.printStackTrace();
		}
		if (readNo.get()>=(round*total-1)) {
			System.out.println("read end time:"+new Date());
			System.out.println("read use sec:"+(new Date().getTime()-start.getTime())/1000);
		}
//		System.out.println("read end time:"+new Date());
//		System.out.println("write use sec:"+(new Date().getTime()-start.getTime())/1000);
	}
};
	

}
